//响应次数
let ajaxTimer = 0

export const request = (params) => {
	let header = {...params.header}
	if(params.url.includes('/my/')){
		header['Authorization'] = wx.getStorageSync("token")
	}
	
	ajaxTimer++;
	wx.showLoading({
		title: '加载中',
		mask: true
	})

	const baseURL = 'https://api-hmugo-web.itheima.net/api/public/v1'
	return new Promise((resovle, reject) => {
		wx.request({
			...params,
			header:header,
			url: baseURL + params.url,
			success: (res) => {
				resovle(res)
			},
			fail: (err) => {
				reject(err)
			},
			complete: () => {
				ajaxTimer--;
				if (ajaxTimer == 0) {
					wx.hideLoading()
				}
			}
		})
	})
}

export const login =()=>{
	return new Promise((resovle,reject)=>{
		wx.login({
			timeout:10000,
			success:(res) => {
				resovle(res)
			},
			fail:(err)=>{
				reject(err)
			}
		})
	})
}


export const requestPayment = (pay)=>{
	return new Promise((resovle,reject)=>{
		wx.requestPayment({
			  ...pay,
			  success :(res) =>{
				  resovle(res)
			  },
			  fail :(err) =>{
				  reject(err)
			  }
			})
	})
}


